﻿using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using appInterfaces;
using appDataContracts;
using System;


namespace appSupplier.UserControls
{
    /// <summary>
    /// Logique d'interaction pour UCAccount.xaml
    /// </summary>
    public partial class UCAccount : UserControl
    {

        public UCAccount()
        {
            InitializeComponent();

            #region Remplissage des TextBox avec les infos du supplier
            tbName.Text = AppWindow.supplier.SUPPLIERNAME;
            tbEmail.Text = AppWindow.supplier.SUPPLIERMAIL;
            tbAddress.Text = AppWindow.supplier.SUPPLIERADRESS;
            tbZip.Text = AppWindow.supplier.SUPPLIERZIP;
            tbCity.Text = AppWindow.supplier.SUPPLIERCITY;
            tbCountry.Text = AppWindow.supplier.SUPPLIERCOUNTRY;
            tbPhone.Text = AppWindow.supplier.SUPPLIERPHONE;
            #endregion

            #region Repmlissage des tags pour garder en mémoire l'état précédent

            foreach (Control control in grid.Children)
            {
                if (control.GetType() == typeof(TextBox))
                {
                    control.Tag = ((TextBox)control).Text;
                }
            }
            #endregion
        }

        private void btCancel_Click(object sender, RoutedEventArgs e)
        {
            #region Retour à l'état précédent
            foreach (Control control in grid.Children)
            {
                if (control.GetType() == typeof(TextBox))
                {
                    ((TextBox)control).Text = control.Tag.ToString();
                }
            } 
            #endregion

            lblErrorMail.Content = "";
            lblErrorName.Content = "";
            lblError.Content = "";
        }

        private void btSave_Click(object sender, RoutedEventArgs e)
        {

            #region Regex email
            string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""]+"
                 + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
                 + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
                 + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
                 + @"[a-zA-Z]{2,}))$";
            Regex regMail = new Regex(patternStrict); 
            #endregion

            if (!regMail.IsMatch(tbEmail.Text) )
            {
                lblErrorMail.Content = "Email incorrect.";
            }
            else if (tbName.Text == "")
            {
                lblErrorName.Content = "Nom requis.";
            }
            else
            {
                lblErrorMail.Content = "";
                lblError.Content = "";
                lblErrorName.Content = "";
                #region Repmlissage des tags pour garder en mémoire l'état précédent
                foreach (Control control in grid.Children)
                {
                    if (control.GetType() == typeof(TextBox))
                    {
                        control.Tag = ((TextBox)control).Text;
                    }
                }
                #endregion

                try
                {
                    AppWindow.supplierService.updateSupplier(
                                new SerializedSupplier(
                                    AppWindow.supplier.SUPPLIERID,
                                    tbName.Text,
                                    tbAddress.Text,
                                    tbZip.Text,
                                    tbCity.Text,
                                    tbCountry.Text,
                                    tbEmail.Text,
                                    tbPhone.Text,
                                    AppWindow.supplier.SUPPLIERPASSWORD,
                                    AppWindow.supplier.SUPPLIERDATELIMIT));
                }
                catch (Exception ex)
                {
                    lblError.Content = "Problème de connexion.";
                }
            }
        }

        private void btPassword_Click(object sender, RoutedEventArgs e)
        {
            new PasswordWindow().ShowDialog();
        }

    }
}
